import pygame


class ScoreBoard:
    def __init__(self, bullets, spiders, screen):
        self.score = 0
        self.bullets = bullets
        self.spiders = spiders
        self.screen = screen
        self.screen_rect = screen.get_rect()
        self.font = pygame.font.SysFont(None, 30)
        self.score_img = self.font.render(str(self.score), True, (250, 250, 250))

        self.score_img_rect = self.score_img.get_rect()
        self.score_img_rect.right = self.screen_rect.right - 30
        self.score_img_rect.top = 20

        self.timer = 0
        self.timer_img = self.font.render("%d S" % self.timer, True, (250, 250, 250))
        self.timer_img_rect = self.timer_img.get_rect()
        self.timer_img_rect.right = self.score_img_rect.right + 15
        self.timer_img_rect.top = 80
        self.brush_time = 1000
        pygame.time.set_timer(pygame.USEREVENT + 2, 1000)

    def blit_scoreboard(self, _a=True):
        if _a:
            self.screen.blit(self.score_img, self.score_img_rect)
        self.screen.blit(self.timer_img, self.timer_img_rect)

    def update_score(self):
        d = pygame.sprite.groupcollide(self.bullets, self.spiders, True, True)
        if d:
            self.score += 1
            print("score: %d" % self.score)
            self.score_img = self.font.render(str(self.score), True, (250, 250, 250))
        self.screen.blit(self.score_img, self.score_img_rect)

    def update_timer(self):
        self.timer += 1
        if self.brush_time > 50:
            self.brush_time -= 10
            pygame.time.set_timer(pygame.USEREVENT + 1, self.brush_time)
        self.timer_img = self.font.render("%d S" % self.timer, True, (250, 250, 250))
        self.screen.blit(self.timer_img, self.timer_img_rect)

